<?php
/**
 * desc Created by wang.
 * User: wang
 * motto: 是时候为小时候吹过的牛而努力了!
 * Date: 2020/3/18 9:37
 */

namespace app\admin\controller;


use app\common\lib\Arr;
use think\facade\View;
use app\admin\validate\Auth as AuthValidate;
use app\admin\business\Auth as BusAuth;

class Auth extends AdminBase
{

    /**
     * 功能说明 菜单栏首页
     * @author: wang
     * @Date: 2020/3/23 22:28
     * @return string|\think\response\Json
     * @throws \Exception
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $BusAuth = new BusAuth();
            $limit = $this->request->param('limit', '', 'trim,htmlspecialchars')??5000;
            $list = $BusAuth->getAuthList($limit);
            return $this->show(config('status.lay_table'), 'success', $list);
        }
        return View::fetch('index');
    }

    /**
     * 功能说明 添加菜单栏
     * @author: wang
     * @Date: 2020/3/23 22:28
     * @return string|\think\response\Json
     * @throws \Exception
     */
    public function add()
    {
        $BusAuth = new BusAuth();
        if ($this->request->isPost()) {
            $pid = $this->request->param('pid', '', 'trim,htmlspecialchars');
            $title = $this->request->param('title', '', 'trim,htmlspecialchars');
            $name = $this->request->param('name', '', 'trim,htmlspecialchars');
            $type = $this->request->param('type', '', 'trim,htmlspecialchars');
            $icon = $this->request->param('icon', '', 'trim,htmlspecialchars');
            $sort = $this->request->param('sort', '', 'trim,htmlspecialchars');

            $data = [
                'pid' => $pid,
                'title' => $title,
                'name' => $name,
                'type' => $type,
                'icon' => $icon,
                'sort' => $sort,
            ];
            $AuthValidate = new AuthValidate();
            if (!$AuthValidate->scene('add')->check($data)) {
                return $this->show(config('status.error'), $AuthValidate->getError());
            }


            try {
                $result = $BusAuth::add($data);
            } catch (\Exception $e) {
                return $this->show(config('status.error'), $e->getMessage());
            }

            if ($result == true) {
                return $this->result(config('status.success'), '添加成功', '/admin/auth/index');
            } else {
                return $this->show(config('status.error'), '添加失败');
            }
        }
        $data = $BusAuth::getAuthList();
        return View::fetch('save', [
            'admin_rule' => $data['data']
        ]);
    }

    /**
     * 功能说明 通过ID更新权限
     * @author: wang
     * @Date: 2020/3/24 18:06
     * @return string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $id = $this->request->param('id', '', 'trim,htmlspecialchars');
        $BusAuth = new BusAuth();
        if ($this->request->isPost()) {
            $title = $this->request->param('title', '', 'trim,htmlspecialchars');
            $name = $this->request->param('name', '', 'trim,htmlspecialchars');
            $type = $this->request->param('type', '', 'trim,htmlspecialchars');
            $icon = $this->request->param('icon', '', 'trim,htmlspecialchars');
            $sort = $this->request->param('sort', '', 'trim,htmlspecialchars');

            $data = [
                'title' => $title,
                'name' => $name,
                'type' => $type,
                'icon' => $icon,
                'sort' => $sort,
            ];
            $AuthValidate = new AuthValidate();
            if (!$AuthValidate->scene('edit')->check($data)) {
                return $this->show(config('status.error'), $AuthValidate->getError());
            }

            try {
                $result = $BusAuth::edit($id, $data);
            } catch (\Exception $e) {
                return $this->show(config('status.error'), $e->getMessage());
            }

            if ($result !== false) {
                return $this->result(config('status.success'), '更新成功', '/admin/auth/index');
            } else {
                return $this->show(config('status.error'), '更新失败');
            }
        }

        $data = $BusAuth::getDataById($id);
        return View::fetch('save', [
            'data' => $data,
        ]);
    }

    public function del()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id', '', 'trim,htmlspecialchars');
            $data = [
                'id' => $id,
            ];
            $AuthValidate = new AuthValidate();
            if (!$AuthValidate->scene('del')->check($data)) {
                return $this->show(config('status.error'), $AuthValidate->getError());
            }
            $BusAuth = new BusAuth();
            try {
                $result = $BusAuth::delDataById($id);
            } catch (\Exception $e) {
                return $this->show(config('status.error'), $e->getMessage());
            }

            if ($result !== false) {
                return $this->result(config('status.success'), '删除成功', '/admin/auth/index');
            } else {
                return $this->show(config('status.error'), '删除失败');
            }
        }
    }

    /**
     * 功能说明 更新排序
     * @author: wang
     * @Date: 2020/3/24 17:05
     * @return \think\response\Json
     */
    public function upSort()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id', '', 'trim,htmlspecialchars');
            $sort = $this->request->param('sort', '', 'trim,htmlspecialchars');
            $data = [
                'id' => $id,
                'sort' => $sort,
            ];
            $AuthValidate = new AuthValidate();
            if (!$AuthValidate->scene('upSort')->check($data)) {
                return $this->show(config('status.error'), $AuthValidate->getError());
            }

            $BusAuth = new BusAuth();
            try {
                $result = $BusAuth::upSort($data);
            } catch (\Exception $e) {
                return $this->show(config('status.error'), $e->getMessage());
            }

            if ($result == true) {
                return $this->result(config('status.success'), '更新成功', '/admin/auth/index');
            } else {
                return $this->show(config('status.error'), '更新失败');
            }
        }
    }


}